<?php include_once(dirname(__FILE__).'/../include/config.inc.php');
$action = $_GET['a'];
switch ($a){
    case 'gettop':
        $list = gettop($dosql,$dopage);
        break;
    case 'list':
        $list = getlist($dosql,$dopage,$_GET);
        break;
    case 'recommendData':
        getrecommend($dosql,$dopage);
        break;
}

function gettop($dosql,$dopage){
        $info = [];
    $list = [];
    $sql = "SELECT * FROM `#@__infolist` WHERE flag='h' and  delstate <> true and checkinfo=true ORDER BY orderid DESC ";
    $dopage->GetPage($sql,5);
    while($row = $dosql->GetArray()) {
        $info['title'] = $row['title'];
        $info['headerImageUrl'] = $row['picurl'];
        $info['link'] = "/newsinfo.php?pid=".$row['id'];
        $info['createTime'] = date("d/m.Y",$row['posttime']);
        $list[] = $info;
    }
    $ret = [
        'errcode'   => '0',
        'data'  => [
            'articles'  =>$list
        ],
    ];
    echo json_encode($ret,true);
    die();
}
function getlist($dosql,$dopage){
    $refer = $_SERVER['HTTP_REFERER'];
    $data = parse_url($refer);
    if(isset($data['query'])){
        $queryData = explode('&', $data['query']);
        $qqData = array();
        foreach ($queryData as $k => $v) {
            //将参数再次分割
            $str = explode('=', $v);
            //参数赋值
            $qqData[$str[0]] = $str[1];
        }
    }else{
        $qqData = [];
    }
    $info = [];
    $list = [];
    $sql = "SELECT * FROM `#@__infolist` WHERE delstate <> true and checkinfo=true ";
    $typearray = [
        1   =>  "and flag='h'",
        2   =>  "and classid=14",
        3   =>  "and classid=15"
    ];

    if(isset($_GET['type'])){
        $sql .= $typearray[(int)$_GET['type']];
    }elseif (isset($qqData['cid'])){
        $sql .= "and classid=".(int)$qqData['cid'];
    }else{
        $sql .= "and flag='h'";
    }
    $dopage->GetPage($sql,1);
    while($row = $dosql->GetArray()) {
        for($i=0;$i<10;$i++) {
            $info['author'] = $row['author'];
            $info['cover'] = $row['picurl'];
            $info['link'] = "/newsinfo.php?pid=" . $row['id'];
            $info['createTime'] = (int)$row['posttime'];
            $info['title'] = $row['title'];
            $info['summary'] = $row['description'];
            $info['views'] = $row['hits'];
            $info['authorAvatar'] = $row['picurl'];
            $info['tid'] = 3;
            $list[] = $info;
        }
    }
    $ret = [
        'errcode'   => '0',
        'data'  => [
            'articles'  =>$list,
            'currentPage'   =>$dopage->page ,
            'pageCount' =>  $dopage->totalpage ,
            'total'     =>$dopage->total * 10,
        ],
    ];
    echo json_encode($ret,true);
    die();

}

function getrecommend($dosql,$dopage){
    $info = [];
    $list = [];
    filter_input(INPUT_GET,'type',FILTER_SANITIZE_NUMBER_INT);
    switch ($_GET['type']){
        case 7:
            $classid = 20;
            break;
        case 4:
            $classid = 14;
            break;
        default:
            $classid = 4;
            break;
    }
    $sql = "SELECT * FROM `#@__infolist` WHERE classid = $classid and  delstate <> true and checkinfo=true ORDER BY orderid DESC ";
    $dopage->GetPage($sql,5);
    while($row = $dosql->GetArray()) {
        $info['title'] = $row['title'];
        $info['thumb'] = $row['picurl'];
        $info['url'] = "/newsinfo.php?pid=".$row['id'];
        $list[] = $info;
    }
    $ret = [
        'errcode'   => '0',
        'data'  => $list,
    ];
    echo json_encode($ret,true);
    die();
}